function HandleTableRowPurchase(gui, item, options) {
  this._gui = gui;
  this._item = item;
  this._options = options;
  this._popupBox = new PopupBox();
  this._popupBox2 = new PopupBox2();


  this._parent = (options && options.parent) ? options.parent : null;
  this._samples = (options && options.samples) ? options.samples : [];
  this._callbackFunc = (options && options.callbackFunc) ? options.callbackFunc : function() {};
  
  this._hidePrice = (options && options.hidePrice) ? options.hidePrice : false;
  this._noEdit = (options && options.noEdit) ? options.noEdit : false;
  this._cancelOnly = (options && options.cancelOnly) ? options.cancelOnly : false;
  this._isPackage = (options && options.isPackage) ? options.isPackage : false;
  
  if (!this._isPackage) {
    this._total = parseFloat(this._item.total, 10);
  }
  this._createElements();
};

HandleTableRowPurchase.prototype._createElements = function() {
  var a, img, _self = this;
  DlmanageUtils.clear(this._gui);
  
  if (this._isPackage) {
    var span = document.createElement('span');
    span.style.verticalAlign = 'middle';
    span.appendChild(document.createTextNode(this._item.sample.name));
    this._gui.name.appendChild(span);
    
    this._gui.qty.appendChild(document.createTextNode(this._item.quantity));
    this._gui.unit.appendChild(document.createTextNode(this._item.sample.unit));
    this._gui.price.appendChild(document.createTextNode(((this._item.sample.price * this._item.quantity > 0) ? '$' + (this._item.sample.price * this._item.quantity * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00')));
    
    img = document.createElement('img');
    img.style.margin = '0 0 0 5px';
    img.src = 'images/common/edit2.png';
    img.style.cursor = 'pointer';
    img.onclick = function() { _self._editPackage.call(_self, DOMUtils.findPos(this)); };
    this._gui.name.appendChild(img);
    
    img = document.createElement('img');
    img.src = 'images/common/delete.png';
    img.style.cursor = 'pointer';
    img.onclick = function() { MiscUtils.remove(_self._parent.packageItems, _self._item, 'trackId');
                               _self._callbackFunc();
                             };
    this._gui.remove.appendChild(img);
    
  } else {
   
    if (this._item.cancelled == 1) {
      this._gui.name.style.textDecoration = 'line-through';
      this._gui.qty.style.textDecoration = 'line-through';
      this._gui.unit.style.textDecoration = 'line-through';
      this._gui.price.style.textDecoration = 'line-through';
    }
    
   
//    achg = document.createElement('a');
//    achg.style.fontSize = '10px';
//    achg.href = '#';
//    achg.style.margin = '0 10px 0 0';
//    achg.appendChild(document.createTextNode('Chg'));
    
//        achg.onclick = function() {  var pos = DOMUtils.findPos(this);
                               //var div = document.createElement('div');
                               
                               //var oNote =[] ;
                               //var _onotecalcel = this._onotecalcel
                              // var func1 = function() { 
                                            //            if(oNote._text == '')
                                            //            {
                                           //                window.alert('CAN NOT BE EMPTY');
                                         //                  return ;
                                       //                 }
                                     //                   var cusnote = {};
                                                        //cusnote.note = oNote._text;
                                                        //cusnote.Operator = _self._operator.account;
                                                        //cusnote.date = _self._now;
                                                        //cusnote.customerid = _self._customer.id;
                                                        //cusnote.id = _onotecalcel.id;
                                                        
                                                        //function() { _self._retrieveCustomer.call(_self); }
                                                        //new RequestUtils()._mwrite('v1office', [cusnote], [],function() { _self._retrieveCustomer.call(_self); } , { pos: pos });
                                   //                     _self._popupBox2._close();
                                 //                     };
                               //var func2 = function() { _self._popupBox2._close(); };
                               //MiscUtils.dialog(_self._popupBox2, pos, div, func1, func2, {t: 'CHANGE PRODUCT'});
                               //new DialogInput(div, oNote, { title: this._item.id });
                               //return false;
                               //alert(_self._item.id);
                              // }; 
                               
    achg = document.createElement('input');
    achg.type = 'radio';
    achg.style.fontSize = '10px';
    achg.name = 'chgproduce';
    achg.id = 'chgproduce';
    achg.value = _self._item.id;
    //achg.href = '#';
    
    achg.style.margin = '0 10px 0 0';
    achg.appendChild(document.createTextNode(achg));                      

    
    var span = document.createElement('span');
    span.style.verticalAlign = 'middle';
    span.appendChild(document.createTextNode(this._item.name));
    //this._gui.name.appendChild(achg);
    this._gui.name.appendChild(span);
   
   //console.log(this._item); 
   //console.log(this._parent);
    this._gui.qty.appendChild(document.createTextNode(this._item.quantity));
    this._gui.unit.appendChild(document.createTextNode(this._item.unit));
    if (this._hidePrice) {
      if (this._item.cancelled == 1) {
        this._gui.price.style.color = '#ff0000';
        var disc1 = 0; disc2 = 0;
        if (this._item.packageInc == '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}') {
          if (this._parent.photooriginal == 0) {
            disc1 = 1;
          } else {
            disc1 = Math.abs(1 - ((this._parent.photooriginal - this._parent.photoprice) / this._parent.photooriginal));
          }
          this._gui.price.appendChild(document.createTextNode('-$' + parseFloat(this._item.total * disc1, 10).toFixed(2)));
        } else {
          if (this._parent.videooriginal == 0) {
            disc2 = 1;
          } else {
            disc2 = Math.abs(1 - ((this._parent.videooriginal - this._parent.videoprice) / this._parent.videooriginal));
          }
          this._gui.price.appendChild(document.createTextNode('-$' + parseFloat(this._item.total * disc2, 10).toFixed(2)));
        }
      }
    } else {
      this._gui.price.appendChild(document.createTextNode(((this._item.total > 0) ? '$' + (this._item.total * parseFloat(1 + parseFloat('{{$smarty.const.GST|escape:javascript}}'), 10)).toFixed(2) + ' (Inc. ' + '{{$smarty.const.TAX|escape:javascript}}' + ')' : '$0.00') + ((this._item.total != (this._item.price * this._item.quantity + ((!this._isPackage) ? ((this._item.isFree && (this._item.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || this._item.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}')) ? parseFloat(180) : parseFloat(0)) : parseFloat(0))) ? (' (' + ((((this._item.price *  this._item.quantity + ((!this._isPackage) ? ((this._item.isFree && (this._item.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || this._item.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}')) ? parseFloat(180) : parseFloat(0)) : parseFloat(0))) - this._item.total) / (this._item.price *  this._item.quantity + ((!this._isPackage) ? ((this._item.isFree && (this._item.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || this._item.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}')) ? parseFloat(180) : parseFloat(0)) : parseFloat(0)))) * 100).toFixed(1) + '% Off)') : ''))));
    }
    if ((this._parent.disposalstatus == 0) && (this._parent.finished == 0)) {
      if (this._item.cancelled == 0) {
        img = document.createElement('img');
        img.style.margin = '0 0 0 5px';
        img.src = 'images/common/edit2.png';
        img.style.cursor = 'pointer';
        img.onclick = function() { _self._editPurchase.call(_self, DOMUtils.findPos(this)); };
        this._gui.name.appendChild(img);
        
        img = document.createElement('img');
        img.src = 'images/common/delete.png';
        img.style.cursor = 'pointer';
        img.onclick = function() {//if (_self._item.isFree && (_self._item.type == '{{$smarty.const.SAMPLE_TYPE_ALBUM|escape:javascript}}' || _self._item.type == '{{$smarty.const.SAMPLE_TYPE_MAGAZINE|escape:javascript}}')) {
//                                     var r = window.confirm('Remove Free Design Album, Continue?');
//                                     if (r) {
//                                       if (_self._item.packageInc == '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}') {
//                                         if (_self._parent.photooriginal == 0) {
//                                           var disc = 1;
//                                         } else {
//                                           var disc = Math.abs(1 - ((_self._parent.photooriginal - _self._parent.photoprice) / _self._parent.photooriginal));
//                                         }
//                                         var total = _self._item.total * disc;
//                                       } else if (_self._item.packageInc == '{{$smarty.const.PACKAGE_TYPE_VIDEO|escape:javascript}}') {
//                                         if (_self._parent.videooriginal == 0) {
//                                           var disc1 = 1;
//                                         } else {
//                                           var disc1 = Math.abs(1 -((_self._parent.videooriginal - _self._parent.videoprice) / _self._parent.videooriginal));
//                                         }
//                                         var total = _self._item.total * disc1;
//                                       } else {
//                                         var total = _self._item.total;
//                                       }
//                                       if (_self._item.packageInc == Constant.PACKAGE_TYPE_PHOTO) {
//                                         var limit = _self._parent.photoprice;
//                                         if (_self._parent.photooriginal == 0) {
//                                           var disc2 = 1;
//                                         } else {
//                                           var disc2 = Math.abs(1 - ((_self._parent.photooriginal - _self._parent.photoprice) / _self._parent.photooriginal));
//                                         }
//                                         for (var i = 0, il = _self._parent.purchaseItems.length; i < il; i++) {
//                                           var p = _self._parent.purchaseItems[i];
//                                           if (p.packageInc == Constant.PACKAGE_TYPE_PHOTO && p.cancelled == 1) {
//                                             limit -= p.total * disc2;
//                                           }
//                                         }
//                                         if (limit <= 0) {
//                                           total = 0;
//                                         } else {
//                                           if (parseFloat(limit, 10) >= parseFloat(total, 10)) {
//                                             total = total;
//                                           } else {
//                                             total = limit;
//                                           }
//                                         }
//                                       } else if (_self._item.packageInc == Constant.PACKAGE_TYPE_VIDEO) {
//                                         var limit = _self._parent.video.price;
//                                         if (_self._parent.videooriginal == 0) {
//                                           var disc3 = 1;
//                                         } else {
//                                           var disc3 = Math.abs(1 - ((_self._parent.videooriginal - _self._parent.videoprice) / _self._parent.videooriginal));
//                                         }
//                                         for (var i = 0, il = _self._parent.purchaseItems.length; i < il; i++) {
//                                           var p = _self._parent.purchaseItems[i];
//                                           if (p.packageInc == Constant.PACKAGE_TYPE_VIDEO && p.cancelled == 1) {
//                                             limit -= p.total * disc3;
//                                           }
//                                         }
//                                         if (limit <= 0) {
//                                           total = 0;
//                                         } else {
//                                           if (parseFloat(limit, 10) >= parseFloat(total, 10)) {
//                                             total = total;
//                                           } else {
//                                             total = limit;
//                                           }
//                                         }
//                                       }
//                                       if (!_self._isPackage) {
//                                         _self._parent.price -= parseFloat(total, 10).toFixed(2);
//                                       }
//                                       
//                                       if (_self._item.cancelled == 1 || _self._item.id) {
//                                         _self._item.cancelled = 1;
//                                       } else {
//                                         if (_self._cancelOnly) {
//                                           _self._item.cancelled = 1;
//                                         } else {
//                                           MiscUtils.remove(_self._parent.purchaseItems, _self._item, 'trackId');
//                                         }
//                                       }
//                                       _self._callbackFunc();
//                                     }
//                                   } else {
                                     if (_self._item.packageInc == '{{$smarty.const.PACKAGE_TYPE_PHOTO|escape:javascript}}') {
                                       if (_self._parent.photooriginal == 0) {
                                         var disc = 1;
                                       } else {
                                         var disc = Math.abs(1 - ((_self._parent.photooriginal - _self._parent.photoprice) / _self._parent.photooriginal));
                                       }
                                       var total = parseFloat(_self._item.total * disc, 10);
                                     } else if (_self._item.packageInc == '{{$smarty.const.PACKAGE_TYPE_VIDEO|escape:javascript}}') {
                                       if (_self._parent.videooriginal == 0) {
                                         var disc1 = 1;
                                       } else {
                                         var disc1 = Math.abs(1 -((_self._parent.videooriginal - _self._parent.videoprice) / _self._parent.videooriginal));
                                       }
                                       var total = parseFloat(_self._item.total * disc1, 10);
                                     } else {
                                       var total = parseFloat(_self._item.total, 10);
                                     }
                                     
                                     if (_self._item.packageInc == Constant.PACKAGE_TYPE_PHOTO) {
                                       var limit = _self._parent.photoprice;
                                       if (_self._parent.photooriginal == 0) {
                                         var disc2 = 1;
                                       } else {
                                         var disc2 = Math.abs(1 - ((_self._parent.photooriginal - _self._parent.photoprice) / _self._parent.photooriginal));
                                       }
                                       for (var i = 0, il = _self._parent.purchaseItems.length; i < il; i++) {
                                         var p = _self._parent.purchaseItems[i];
                                         if (p.packageInc == Constant.PACKAGE_TYPE_PHOTO && p.cancelled == 1) {
                                           limit -= p.total * disc2;
                                         }
                                       }
                                       if (limit <= 0) {
                                         total = 0;
                                       } else {
                                         if (parseFloat(limit, 10) >= parseFloat(total, 10)) {
                                           total = total;
                                         } else {
                                           total = limit;
                                         }
                                       }
                                       
                                     } else if (_self._item.packageInc == Constant.PACKAGE_TYPE_VIDEO) {
                                       var limit = _self._parent.videoprice;

                                       if (_self._parent.videooriginal == 0) {
                                         var disc3 = 1;
                                       } else {
                                         var disc3 = Math.abs(1 - ( (_self._parent.videooriginal - _self._parent.videoprice ) / _self._parent.videooriginal ) );
                                       }
                                       for (var i = 0, il = _self._parent.purchaseItems.length; i < il; i++) {
                                         var p = _self._parent.purchaseItems[i];
                                         if (p.packageInc == Constant.PACKAGE_TYPE_VIDEO && p.cancelled == 1) {
                                           limit -= p.total * disc3;
                                         }
                                       }
                                       
                                       if (limit <= 0) {
                                         total = 0;
                                       } else {
                                         if (parseFloat(limit, 10) >= parseFloat(total, 10)) {
                                           total = total;
                                         } else {
                                           total = limit;
                                         }
                                       }
                                     }
                                     if (!_self._isPackage) {
                                       _self._parent.price -= parseFloat(total, 10);
                                       if(_self._item.model==Constant.MODEL_NAME_USER_PHOTOGRAPHER || _self._item.model==Constant.MODEL_NAME_PRODUCT_COMMON || _self._item.model==Constant.MODEL_NAME_USER_MAKEUP ||
                                          _self._item.model==Constant.MODEL_NAME_USER_LIGHT || _self._item.model==Constant.MODEL_NAME_PRODUCT_4X6 || _self._item.model==Constant.MODEL_NAME_PRODUCT_RETOUCH ||
                                          _self._item.model==Constant.MODEL_NAME_PRODUCT_RETOUCH_MAGAZINE || _self._item.model=='')  
                                          {                         
                                            _self._parent.pgprice = parseFloat(parseFloat(_self._parent.pgprice, 10) -  parseFloat(total, 10),10);
                                          }

                                       if(_self._item.model==Constant.MODEL_NAME_USER_VIDEOGRAPHER || _self._item.model==Constant.MODEL_NAME_PRODUCT_VIDEO || _self._item.model==Constant.MODEL_NAME_PRODUCT_VIDEODVD ||
                                          _self._item.model==Constant.MODEL_NAME_PRODUCT_VIDEO_BLURAY || _self._item.model==Constant.MODEL_NAME_PRODUCT_VIDEODVD_BLURAY || _self._item.model==Constant.MODEL_NAME_PRODUCT_SLIDESHOW ||
                                          _self._item.model==Constant.MODEL_NAME_PRODUCT_SLIDESHOW_OL || _self._item.model==Constant.MODEL_NAME_PRODUCT_SLIDESHOWW )  
                                          {                         
                                            _self._parent.vgprice = parseFloat(parseFloat(_self._parent.vgprice, 10) -  parseFloat(total, 10),10);
                                          }
                                          
                                     }
                                     if (_self._item.cancelled == 1 || _self._item.id) {
                                       _self._item.cancelled = 1;
                                     } else {
                                       if (_self._cancelOnly) {
                                         _self._item.cancelled = 1;
                                       } else {
                                         MiscUtils.remove(_self._parent.purchaseItems, _self._item, 'trackId');
                                       }
                                     }
                                     _self._callbackFunc();
//                                   }
                                 };
        this._gui.remove.appendChild(img);
      }
    }
  }
};

HandleTableRowPurchase.prototype._editPackage = function(pos) {
  var div, _self = this;
  var func1 = function() { _self._popupBox2._close();
                           _self._total = parseFloat(_self._item.total, 10);
                           _self._callbackFunc();
                         };
  var func2 = function() { _self._popupBox2._close();
                           _self._createElements.call(_self);
                         };
  var div = document.createElement('div');
  MiscUtils.dialog(this._popupBox2, pos, div, func1, func2, {t: this._item.sample.name, width: 800, height: 50});
  var iContainerPurchase = new IContainerPurchase(div, this._item, { callbackFunc: function() { iContainerPurchase._createElements(); }, parent: this._parent, samples: _self._samples, noEdit: this._noEdit, hidePrice: this._hidePrice, isPackage: this._isPackage, cancelOnly: this._cancelOnly});
};

HandleTableRowPurchase.prototype._editPurchase = function(pos) {
  var div, _self = this;
  var func1 = function() { _self._popupBox2._close();
                            if (!_self._isPackage) {
                              _self._parent.price -= _self._total;
                              _self._parent.price += parseFloat(_self._item.total, 10);
                                       if(_self._item.model==Constant.MODEL_NAME_USER_PHOTOGRAPHER || _self._item.model==Constant.MODEL_NAME_PRODUCT_COMMON || _self._item.model==Constant.MODEL_NAME_USER_MAKEUP ||
                                          _self._item.model==Constant.MODEL_NAME_USER_LIGHT || _self._item.model==Constant.MODEL_NAME_PRODUCT_4X6 || _self._item.model==Constant.MODEL_NAME_PRODUCT_RETOUCH ||
                                          _self._item.model==Constant.MODEL_NAME_PRODUCT_RETOUCH_MAGAZINE || _self._item.model=='')  
                                          {                         
                                            _self._parent.pgprice = parseFloat(parseFloat(_self._parent.pgprice, 10) -  parseFloat(_self._total, 10),10);
                                            _self._parent.pgprice = parseFloat(parseFloat(_self._parent.pgprice, 10) +  parseFloat(_self._item.total, 10),10);
                                          }

                                       if(_self._item.model==Constant.MODEL_NAME_USER_VIDEOGRAPHER || _self._item.model==Constant.MODEL_NAME_PRODUCT_VIDEO || _self._item.model==Constant.MODEL_NAME_PRODUCT_VIDEODVD ||
                                          _self._item.model==Constant.MODEL_NAME_PRODUCT_VIDEO_BLURAY || _self._item.model==Constant.MODEL_NAME_PRODUCT_VIDEODVD_BLURAY || _self._item.model==Constant.MODEL_NAME_PRODUCT_SLIDESHOW ||
                                          _self._item.model==Constant.MODEL_NAME_PRODUCT_SLIDESHOW_OL || _self._item.model==Constant.MODEL_NAME_PRODUCT_SLIDESHOWW )  
                                          {                         
                                            _self._parent.vgprice = parseFloat(parseFloat(_self._parent.vgprice, 10) -  parseFloat(_self._total, 10),10);
                                            _self._parent.vgprice = parseFloat(parseFloat(_self._parent.vgprice, 10) +  parseFloat(_self._item.total, 10),10);
                                          }                              
                              
                            }
                           _self._total = parseFloat(_self._item.total, 10);
                           _self._callbackFunc();
                         };
  var func2 = function() { _self._popupBox2._close();
                           _self._createElements.call(_self);
                         };
  var div = document.createElement('div');
  MiscUtils.dialog(this._popupBox2, pos, div, func1, func2, {t: this._item.name, width: 800, height: 50});
  var iContainerPurchase = new IContainerPurchase(div, this._item, { callbackFunc: function() { iContainerPurchase._createElements(); }, parent: this._parent, samples: _self._samples, noEdit: this._options.noEdit, hidePrice: this._options.hidePrice});
};
